// chooseFile.js文件内容
function chooseFile(callback, acceptType) {
	var CODE_REQUEST = 1000;
	var context = plus.android.runtimeMainActivity();
	var Intent = plus.android.importClass('android.content.Intent');
	var intent = new Intent(Intent.ACTION_GET_CONTENT);

	intent.setType("*/*"); //设置类型，任意类型
	//intent.setType("image/!*");
	//intent.setType("audio/!*"); //选择音频
	//intent.setType("video/!*"); //选择视频 （mp4 3gp 是android支持的视频格式）

	intent.addCategory(Intent.CATEGORY_OPENABLE);
	plus.android.requestPermissions(['android.permission.WRITE_EXTERNAL_STORAGE'], function(e) {
		if (e.deniedAlways.length > 0) { //权限被永久拒绝
			// 弹出提示框解释为何需要定位权限，引导用户打开设置页面开启
		}
		if (e.deniedPresent.length > 0) { //权限被临时拒绝
			// 弹出提示框解释为何需要定位权限，可再次调用plus.android.requestPermissions申请权限
		}
		if (e.granted.length > 0) { //权限被允许
			//调用依赖获取定位权限的代码
		}
	}, function(e) {
		console.error('Request Permissions error:' + JSON.stringify(e));
	});
	context.onActivityResult = function(requestCode, resultCode, data) {
		if (CODE_REQUEST == requestCode) {
			var fileData = data.getData();
			plus.android.importClass(fileData)
			plus.android.importClass(data);
			let DocumentsContract = plus.android.importClass('android.provider.DocumentsContract')

			var docId = DocumentsContract.getDocumentId(fileData)
			var split = docId.split(':')
			var type = split[0]
			let contentUri = null

			if ('com.android.providers.media.documents' == fileData.getAuthority()) {
				var MediaStore = plus.android.importClass('android.provider.MediaStore')
				if ('image' == type) {
					contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
				} else if ('video' == type) {
					contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
				} else if ('audio' == type) {
					contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
				}

				var selection = '_id=?'
				var selectionArgs = new Array()
				selectionArgs[0] = split[1]
				callback(getDataColumn(context, contentUri, selection, selectionArgs))
				return
			}

			// 获得文件路径
			var path = plus.android.invoke(fileData, "getPath");
			console.log("getPath: " + fileData.getPath());

			// 判断文件类型
			var resolver = context.getContentResolver();
			var fileType = plus.android.invoke(resolver, "getType", fileData);

			callback(fileData.getPath())
		}
	}
	context.startActivityForResult(intent, CODE_REQUEST);
	return

	// acceptType 为你要查的文件类型"image/*"，"audio/*"，"video/*;image/*"
	// intent.setType("image/*");
	//intent.setType("audio/*");
	//选择音频
	//intent.setType("video/*;image/*");
	//选择视频 （mp4 3gp 是android支持的视频格式）
	var CODE_REQUEST = 1000
	var context = plus.android.runtimeMainActivity()
	if (plus.os.name == 'Android') {
		var Intent = plus.android.importClass('android.content.Intent')
		var intent = new Intent(Intent.ACTION_GET_CONTENT)
		intent.addCategory(Intent.CATEGORY_OPENABLE)
		if (acceptType) {
			intent.setType(acceptType)
		} else {
			intent.setType('*/*')
		}

		context.onActivityResult = function(requestCode, resultCode, data) {
			if (requestCode == CODE_REQUEST) {
				var uri = data.getData()
				plus.android.importClass(uri)
				var Build = plus.android.importClass('android.os.Build')
				var isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT

				var DocumentsContract = plus.android.importClass('android.provider.DocumentsContract')
				if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
					if ('com.android.externalstorage.documents' == uri.getAuthority()) {
						var docId = DocumentsContract.getDocumentId(uri)
						var split = docId.split(':')
						var type = split[0]

						if ('primary' == type) {
							var Environment = plus.android.importClass('android.os.Environment')
							callback(Environment.getExternalStorageDirectory() + '/' + split[1])
						} else {
							var System = plus.android.importClass('java.lang.System')
							var sdPath = System.getenv('SECONDARY_STORAGE')
							if (sdPath) {
								callback(sdPath + '/' + split[1])
							}
						}
					} else if ('com.android.providers.downloads.documents' == uri.getAuthority()) {
						var id = DocumentsContract.getDocumentId(uri)
						var ContentUris = plus.android.importClass('android.content.ContentUris')
						var MediaStore = plus.android.importClass('android.provider.MediaStore')
						var contentUri = ContentUris.withAppendedId(
							Uri.parse('content://downloads/public_downloads'), id)
						callback(getDataColumn(context, contentUri, null, null))
					} else if ('com.android.providers.media.documents' == uri.getAuthority()) {
						var docId = DocumentsContract.getDocumentId(uri)
						var split = docId.split(':')
						var type = split[0]

						var MediaStore = plus.android.importClass('android.provider.MediaStore')
						if ('image' == type) {
							contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
						} else if ('video' == type) {
							contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
						} else if ('audio' == type) {
							contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
						} else {
						}

						var selection = '_id=?'
						var selectionArgs = new Array()
						selectionArgs[0] = split[1]
						callback(getDataColumn(context, contentUri, selection, selectionArgs))
					}
				} else if ('content' == uri.getScheme()) {
					callback(getDataColumn(context, uri, null, null))
				}
				// File
				else if ('file' == uri.getScheme()) {
					callback(uri.getPath())
				}
			}
		}
		context.startActivityForResult(intent, CODE_REQUEST)
	}
}

function getDataColumn(context, uri, selection, selectionArgs) {
	plus.android.importClass(context.getContentResolver())
	let cursor = context.getContentResolver().query(uri, ['_data'], selection, selectionArgs, null)
	plus.android.importClass(cursor)
	if (cursor != null && cursor.moveToFirst()) {
		var column_index = cursor.getColumnIndexOrThrow('_data')
		var result = cursor.getString(column_index)
		cursor.close()
		return result
	}
	return null
}

export default chooseFile